/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wider.foundation.channel;

import com.wider.foundation.DLogger;
import com.wider.foundation.DLoggerRoot;
import com.wider.foundation.concurrency.Cancellable;
import com.wider.foundation.concurrency.Joinable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author William
 */
public class channelTest{

    public static void main(String args[]) throws InterruptedException{

        //Inner-class
        class ChannelHandler implements NetChannelResponseHandler{
            private DLoggerRoot rootlog;

            public ChannelHandler(DLoggerRoot logger){
                rootlog = logger;
            }

            @Override
            public void handle(String given, int resp_code, String resp_mesg, InputStream stream_in) throws IOException {
                rootlog.debug(given + ": ret code " + resp_code + " " + resp_mesg);
                BufferedReader reader = new BufferedReader(new InputStreamReader(stream_in));
                String line;
                while (null != (line = reader.readLine())){
                    rootlog.debug(given + ": " + line);
                }
            }

            @Override
            public void handle(String given, Throwable ex) {
                rootlog.warn(ex);
            }
        }
        HttpChannels channels = null;

        DLoggerRoot logger = DLogger.getRoot();
        logger.setStream(System.out);
        logger.setLevel(DLogger.DLevel.INFOR);
        logger.enable();

        logger.warn("Hello Channel Tester");

        ChannelHandler handler = new ChannelHandler(logger);

        try {
            channels = new HttpChannels("ChannelGroup-0");
            channels.init(2, 2);

            //Test-1: Possitive Test. Test if echo is successful or not
            HttpChannel channel1 = channels.openChannel("http://localhost:8080/wider_foundation/EchoServlet");

            HashMap<Object, Object> reqst1 = new HashMap<Object, Object>();

            reqst1.put("command", "sleep");
            reqst1.put("timeout", "3000");
            NetChannelRequest ticket = (NetChannelRequest) (Joinable) channel1.get(reqst1, null, 4000);
            logger.debug("Waiting the request to join back.");
            ticket.join();
            logger.debug("Joined back");
            
            logger.debug("Ret code: " + ticket.getResponseCode() + ", Ret mesg: " + ticket.getResponseMessage());
            BufferedReader reader = new BufferedReader(new InputStreamReader(ticket.getInputStream()));
            String line;
            while (null != (line = reader.readLine())){
                logger.debug( ": " + line);
            }

        } catch (MalformedURLException ex) {
            logger.warn(ex);
        } catch (IOException ex) {
            logger.warn(ex);
        } finally {
            //Final termination //normal
            if (null != channels){
                channels.close(0, TimeUnit.DAYS);
            }
        }
    }

}
